const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    imgUrl: null,
    nickName:null,
    array: ['男', '女', '未知'],
    objectArray: [ { id: 0, name: '男' }, { id: 1, name: '女' },{ id: 2, name: '未知'}],
    sex: 0,
    date: '2017-09-01',
    telPhone: '',
    region: ['上海市', '上海市', '松江区'],
    customItem: '全部',
    yourMap:'',
    modalHidden: true
  },
  chooseSex: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      sex: e.detail.value
    })
    wx.setStorage({
      key: 'sex',
      data: e.detail.value,
    })
  },
  // 点击日期组件确定事件  
  chooseDateBirth: function (e) {
    this.setData({
      date: e.detail.value
    })
    wx.setStorage({
      key: 'birthday',
      data: e.detail.value
    })
  },
  onShow: function () {
    var _this = this, nickName = this.data.nickName;
    var telPhone = this.data.telPhone;
    wx.setNavigationBarTitle({
      title: '个人信息'
    })
    wx.getStorage({
      key: "nickName",
      success: function (res) {
        console.log("获取缓存",res.data)
        _this.setData({ 
          nickName: res.data.nickName
        })
      }
    })
    wx.getStorage({
      key: 'sex',
      success: function(res) {
        _this.setData({
          sex: res.data
        })
      },
    })
    wx.getStorage({
      key: 'telPhone',
      success: function(res) {
        _this.setData({
          telPhone: res.data
        })
      },
    })
    wx.getStorage({
      key: 'birthday',
      success: function(res) {
        console.log(res.data)
        _this.setData({
          date: res.data
        })
      }
    })
    wx.getStorage({
      key: 'region',
      success: function(res) {
        console.log(res.data)
        _this.setData({
          region: res.data
        })
      }
    })
    wx.getStorage({
      key: "address",
      success: function (res) {
        console.log(res.data)
        if (res.data) {
          _this.setData({
            yourMap: res.data
          })
        }
      }
    })
    app.getNewTel(telPhone)
    
  },
  onLoad: function () {
    
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    }
    
  },
  //换 头像
  bindViewTap: function () {
    console.log('上传图片');
    var page = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths

        page.setData({ imgUrl: tempFilePaths})
      }
    })
    console.log(imgUrl);
    wx.setStorage({
      key: '',
      data: '',
    })
  },
  editNickName: function(e) {
    wx.navigateTo({
      url: '../nickName/nickName',
    })
  },
  editTelPhone: function() {
    wx.navigateTo({
      url: '../telPhone/telPhone',
    })
  },
  address :function() {
    wx.navigateTo({
      url: '../address/address',
    })
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
    wx.setStorage({
      key: 'region',
      data: e.detail.value,
    })
  },
  
  // 判断手机号是否填充  弹出提示框
  isSwitch: function(e) {
    var Phone = this.data.telPhone;
    // console.log('点击切换',e.detail.value)
    if (Phone == "" || null || undefined) {
      // wx.showModal({
      //   title: '手机号未验证',
      //   showCancel: false,
      //   content: '验证手机号后方可隐藏手机号',
      //   success: function (res) {
      //     console.log(res)
      //   }
      // })

      this.setData({
        modalHidden: !this.data.modalHidden
      })
    }
    
  },
  confirm: function() {
    this.setData({
      modalHidden: !this.data.modalHidden
    })
    wx.navigateTo({
      url: '../telPhone/telPhone',
    })
  },
  cancel: function() {
    this.setData({
      modalHidden: !this.data.modalHidden
    })
  }


})



